package cn.com.surker.system.service.impl;

import cn.com.surker.system.entity.SysReportTime;
import cn.com.surker.system.dao.SysReportTimeDao;
import cn.com.surker.system.service.SysReportTimeService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-07-30
 */
@Service
public class SysReportTimeServiceImpl extends ServiceImpl<SysReportTimeDao, SysReportTime> implements SysReportTimeService {

    @Resource
    private SysReportTimeDao mapper;

    /**
     * 查询所有报表时间
     * @return
     */
    @Cacheable(cacheNames = "calcTodayCache")
    public List<SysReportTime> findTodayAll() {
        return mapper.findTodayAll();
    }
    /**
     * 根据日期查询系统开始结束时间
     *
     * @param today
     * @return
     */
    public SysReportTime findToday(String today) {
        if (StringUtils.isNull(today))
            return null;
        QueryWrapper<SysReportTime> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("today", today);
        query.orderByDesc("id");
        List<SysReportTime> list = mapper.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list.get(0);
    }


    /**
     * @param time
     * @return
     */
    public SysReportTime findTime(String time) {
        if (StringUtils.isNull(time))
            return null;
        QueryWrapper<SysReportTime> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.lt("start_date", time);
        query.ge("end_date", time);
        query.orderByDesc("id");
        List<SysReportTime> list = mapper.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list.get(0);
    }

    /**
     * 根据条件查询所有的数据
     *
     * @param dto
     * @return
     */
    public RestResultDto findAll(SysReportTime dto) {

        QueryWrapper<SysReportTime> query = new QueryWrapper();
        query.setEntity(dto);
        query.orderByDesc("today");
        List<SysReportTime> list = mapper.selectList(query);

        return RestResultDto.success(list);
    }


    /**
     * 分页查询数据
     *
     * @param dto page
     * @return
     */
    public RestResultDto findAllByPage(SysReportTime dto, Page<SysReportTime> page) {
        QueryWrapper<SysReportTime> query = new QueryWrapper();
        query.setEntity(dto);
        IPage<SysReportTime> iPage = mapper.selectPage(page, query);

        return RestResultDto.success(iPage);
    }


    /**
     * 新增数据
     *
     * @param dto page
     * @return
     */
    public RestResultDto saveInit(SysReportTime dto) {
        int saveStatus = mapper.insert(dto);
        if (saveStatus > 0) {
            return RestResultDto.success("保存成功!");
        } else {
            return RestResultDto.success("保存失败!");
        }

    }

    /**
     * 更新数据
     *
     * @param dto page
     * @return
     */
    public RestResultDto update(SysReportTime dto) {

        int saveStatus = mapper.updateById(dto);
        if (saveStatus > 0) {
            return RestResultDto.success("保存成功!");
        } else {
            return RestResultDto.success("保存失败!");
        }

    }
}